package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.activity.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.entity.Artist;

import java.util.ArrayList;
import java.util.List;

public class RecyclerHorAdapter extends RecyclerView.Adapter<RecyclerHorAdapter.RecyclerHorHolder>{
    private Context context;
    private List<Artist> artistList = new ArrayList<>();
    private BaseOnclickListener baseOnclickListener;
    private int limit=-6;
    private static int count = 0;

    public RecyclerHorAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<Artist> artistList){
        this.artistList=artistList;
        count++;
        for(int i=0;i<count;i++){
            limit+=6;
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerHorHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_hor,parent,false);
        return new RecyclerHorHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerHorHolder holder, int position) {
        Artist artist  = artistList.get(position+limit);
        RoundedCorners roundedCorners= new RoundedCorners(30);
        RequestOptions options= RequestOptions.bitmapTransform(roundedCorners).override(300, 300);

        Glide.with(context).load(artist.getImg1v1Url())
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.ic_baseline_refresh_24)
                .placeholder(R.drawable.ic_baseline_refresh_24)
                .apply(options)
                .into(holder.img_recycler_hor);
        holder.tv_name.setText(artist.getName());

        holder.view_pager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(baseOnclickListener !=null){
                    baseOnclickListener.onclick(position+limit);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return artistList == null? 0 : 6;
    }


    public void setBaseOnclickListener(BaseOnclickListener baseOnclickListener){
        this.baseOnclickListener = baseOnclickListener;
    }

    class RecyclerHorHolder extends RecyclerView.ViewHolder {
        ImageView img_recycler_hor;
        TextView  tv_name;
        View view_pager;
        public RecyclerHorHolder(@NonNull View itemView) {
            super(itemView);
            img_recycler_hor = itemView.findViewById(R.id.img_recycler_hor);
            tv_name = itemView.findViewById(R.id.tv_name);
            view_pager = itemView.findViewById(R.id.view_pagers);
        }
    }
}